<template>
  <div class="app-container">
    <div class="register-div">
      {{ form.tipInfo }}
    </div>
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="注册码" prop="code">
        <el-input
          v-model="form.code"
          placeholder="请输入注册码"
          class="register-input"
        />
        <el-button
          type="primary"
          @click="handleRegister"
          v-hasPermi="['system:register:register']"
          >注册</el-button
        >
      </el-form-item>
      <el-form-item label="软件授权" prop="hospName">
        <el-input
          v-model="form.hospName"
          placeholder="用户方实验室名称"
          :disabled="true"
          class="register-input"
        />
      </el-form-item>
      <el-form-item label="授权时限" prop="expirationDate">
        <el-input
          v-model="form.expirationDate"
          placeholder="授权时限"
          :disabled="true"
          class="register-input"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getRegisterInfo, register } from "@/api/system/config";

export default {
  name: "Register",
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表单参数
      form: {
        tipInfo: "",
        code: "",
        hospName: "",
        expirationDate: "",
      },
    };
  },
  created() {
    this.getRegister();
  },
  methods: {
    getRegister() {
      getRegisterInfo().then((response) => {
        this.resetForm(response.data);
      });
    },
    // 表单重置
    resetForm(dataVlaue) {
      const date1 = Date.parse(
        new Date(dataVlaue.expirationDate + " 23:59:59")
      );
      const newDate = new Date().getTime();
      const day = parseInt((date1 - newDate) / (1000 * 60 * 60 * 24));
      this.form.tipInfo = `软件还有${day}天过期，截止时间为${dataVlaue.expirationDate}，请联系工程师重新注册。`;
      this.form.hospName = dataVlaue.hospName;
      this.form.expirationDate = dataVlaue.expirationDate;
    },
    /** 注册按钮操作 */
    handleRegister() {
      if (this.form.code) {
        register(this.form.code).then((response) => {
          this.$modal.msgSuccess("注册成功");
          this.resetForm(response.data);
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.register-input {
  width: 400px;
  margin-right: 10px;
}
.register-div {
  width: 400px;
  margin-left: 100px;
  margin-bottom: 30px;
}
</style>
